<template>
  <a-modal
    :title="title"
    :width="780"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    :destroyOnClose="true"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['name', { rules: [{ required: true, min: 2, message: '请输入至少2个字符' }] }]" />
        </a-form-item>

        <a-form-item label="内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <Ueditor v-decorator="['content', { rules: [{ required: true, message: '内容不能为空' }] }]" />
        </a-form-item>

        <a-form-item label="老师" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            placeholder="请选择一个用户身份"
            v-decorator="['user_id', { rules: [{ required: true, message: '请选择1个用户身份' }] }]"
          >
            <a-select-option v-for="(item, index) in teacherList" :key="index" :value="item.user_id">{{
              item.nick_name
            }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            placeholder="请选择一个类型"
            v-decorator="['type', { rules: [{ required: true, message: '请选择1个类型' }] }]"
          >
            <a-select-option v-for="item in typeList" :key="item.id" :value="item.id">{{ item.type }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="跳转路径" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            placeholder="请选择一个跳转路径"
            v-decorator="['url', { rules: [{ required: true, message: '请选择1个跳转路径' }] }]"
          >
            <a-select-option v-for="item in urlList" :key="item.id" :value="item.message">{{
              item.message
            }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import * as Api from '@/api/content/academy'
import { SelectImage, Ueditor } from '@/components'

export default {
  components: {
    SelectImage,
    Ueditor,
  },
  props: {
    // 分类列表
    teacherList: {
      type: Array,
      required: true,
    },
    // 类型列表
    typeList: {
      type: Array,
      required: true,
    },
    // 路由列表
    urlList: {
      type: Array,
      required: true,
    },
  },

  // mounted() {
  //   console.log('teacherList', this.teacherList)
  // },
  data() {
    return {
      // 对话框标题
      title: '新增文章',
      // 标签布局属性
      labelCol: {
        span: 7,
      },
      // 输入框布局属性
      wrapperCol: {
        span: 13,
      },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
    }
  },
  methods: {
    /**
     * 显示对话框
     */
    add() {
      // 显示窗口
      this.visible = true
    },

    /**
     * 确认按钮
     */
    handleSubmit(e) {
      e.preventDefault()
      // 表单验证
      const {
        form: { validateFields },
      } = this
      validateFields((errors, values) => {
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel() {
      this.visible = false
      this.form.resetFields()
    },

    /**
     * 提交到后端api
     */
    onFormSubmit(values) {
      this.confirmLoading = true
      Api.add(values)
        .then((result) => {
          // 显示成功
          this.$message.success(result.message, 1.5)
          // 关闭对话框事件
          this.handleCancel()
          // 通知父端组件提交完成了
          this.$emit('handleSubmit', values)
        })
        .finally((result) => {
          this.confirmLoading = false
        })
    },
  },
}
</script>
